﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DynaPrecision.Numerical.Mesh
{
	/// <summary>
	/// 
	/// </summary>
	/// <typeparam name="T"></typeparam>
	public class UniformCartesianMesh<T> : ICartesianMeshData<T>
	{
		protected T[] _startPositions = new T[3];

		/// <summary>
		/// the lower/left/front position of the grid
		/// </summary>
		public T[] StartPositions
		{
			get
			{
				return _startPositions;
			}
		}
		protected T[] _differences = new T[3];

		/// <summary>
		/// The delta increment for each dimension
		/// </summary>
		public T[] Differences
		{
			get
			{
				return _differences;
			}
		}
		protected int[] _gridCounts = new int[3];

		/// <summary>
		/// 
		/// </summary>
		public int[] GridCounts
		{
			get
			{
				return _gridCounts;
			}
		}
		#region ICartesianMeshData<T> Members

		public T Difference(int dimension, params int[] index)
		{
			return _differences[dimension];
		}

		#endregion

		#region ICartesianMesh<T> Members

		public int Dimension
		{
			get
			{
				return _gridCounts.Count(_i => _i != 0);
			}
		}

		public LinearSystem.IMultiDimensionalArray<T> this[int dimension]
		{
			get
			{
				LinearSystem.FunctionMappedArray<T> result = new LinearSystem.FunctionMappedArray<T>();
				result.Getter = (_index) => _index[dimension] * (dynamic)_differences[dimension] + _startPositions[dimension];
				result.LengthGetter = (i) => (_gridCounts[i]);
				result.DimensionGetter = () => (_gridCounts.Count(dimen => dimen == 0));
				return result;
			}
		}

		#endregion
	}
}
